
package javacroft.dataccess;

import au.com.bytecode.opencsv.*;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Classe dérivée de gestion de la couche persistance fichier texte
 * @author Fabien RETIF
 * @version 1.0
 */
public class TxtProvider extends DataAccessFactory
{

    public TxtProvider()
    {
        super("./data/");
    }

    public void openDataAccess () throws Exception
    {
    }

    public void closeDataAccess() throws Exception
    {
    }

    public List<Map> getLesDepartements()  throws Exception
    {
        List<Map> res = new ArrayList<Map>();
        Map<String,String> map;
        String[] line;

        CSVReader readerDepartement = new CSVReader(new FileReader(getClass().getResource(this.dataAccessString+"departement.txt").getFile()), ';');

        while((line = readerDepartement.readNext()) != null)
        {
            map  = new HashMap<String, String>();
            map.put("unNom", line[0]);
            res.add(map);
        }
        readerDepartement.close();

        return res;
    }
    public List<Map> getLesAnnees(String nomDepartement)  throws Exception
    {
        List<Map> res = new ArrayList<Map>();
        Map<String,String> map;
        String[] line;

        CSVReader readerAnnee = new CSVReader(new FileReader(this.dataAccessString+"annee.txt"), ';');

        while((line = readerAnnee.readNext()) != null)
        {
            if (line[1].equalsIgnoreCase(nomDepartement))
            {
                map  = new HashMap<String, String>();
                map.put("unNom", line[0]);
                res.add(map);
            }
        }
        readerAnnee.close();
        return res;
    }
    public List<Map> getLesSemestres(String nomAnnee)  throws Exception
    {
        List<Map> res = new ArrayList<Map>();
        Map<String,String> map;
        String[] line;

        CSVReader readerSemestre = new CSVReader(new FileReader(this.dataAccessString+"semestre.txt"), ';');

        while((line = readerSemestre.readNext()) != null)
        {
            if (line[2].equalsIgnoreCase(nomAnnee))
            {
                map  = new HashMap<String, String>();
                map.put("unCodeApogee", line[0]);
                map.put("unNumero", line[1]);
                res.add(map);
            }
        }
        readerSemestre.close();
        return res;
    }
    public List<Map> getLesUE(String nomSemestre)  throws Exception
    {
        List<Map> res = new ArrayList<Map>();
        Map<String,String> map;
        String[] line;

        CSVReader readerUE = new CSVReader(new FileReader(this.dataAccessString+"ue.txt"), ';');

        while((line = readerUE.readNext()) != null)
        {
            if (line[2].equalsIgnoreCase(nomSemestre))
            {
                map  = new HashMap<String, String>();
                map.put("unNom", line[0]);
                map.put("nbCredit", line[1]);
                res.add(map);
            }
        }
        readerUE.close();
        return res;
    }

    @Override
    public List<Map> getLesECUE(String nomUE) throws Exception
    {
        List<Map> res = new ArrayList<Map>();
        Map<String,String> map;
        String[] line;

        CSVReader readerECUE = new CSVReader(new FileReader(this.dataAccessString+"ecue.txt"), ';');

        while((line = readerECUE.readNext()) != null)
        {
            if (line[2].equalsIgnoreCase(nomUE))
            {
                map  = new HashMap<String, String>();
                map.put("unNom", line[0]);
                map.put("unCoeff", line[1]);
                res.add(map);
            }
        }
        readerECUE.close();
        return res;
    }

    public List<Map> getLesEtudiantsAnnee(String nomAnnee) throws Exception
    {
        List<Map> res = new ArrayList<Map>();
        Map<String,String> map;
        String[] line;

        CSVReader readerEtudiant = new CSVReader(new FileReader(this.dataAccessString+"etudiant.txt"), ';');

        while((line = readerEtudiant.readNext()) != null)
        {
            {
                map  = new HashMap<String, String>();
                map.put("unNom", line[0]);
                map.put("unPrenom", line[1]);
                map.put("scoreTOEIC", line[2]);
                map.put("pointJuryAnnee", line[3]);
                map.put("codeSemestre", line[4]);
                map.put("pointJurySemestre", line[5]);
                map.put("estSemestreEtranger", line[6]);
                map.put("nomUE", line[7]);
                map.put("apdjUE", line[8]);
                map.put("nomECUE", line[9]);
                map.put("saNote", line[10]);
                map.put("estRattrapage", line[11]);
                res.add(map);
            }
        }
        readerEtudiant.close();
        return res;
    }

    @Override
    public List<Map> getLesMoyennesEtranger(String nom, String prenom, String codeSemestre) throws Exception
    {
        List<Map> res = new ArrayList<Map>();
        Map<String,String> map;
        String[] line;

        CSVReader readerEtranger = new CSVReader(new FileReader(this.dataAccessString+"etranger.txt"), ';');

        while((line = readerEtranger.readNext()) != null)
        {
            if (line[1].equalsIgnoreCase(nom) && line[2].equalsIgnoreCase(prenom) && line[3].equalsIgnoreCase(codeSemestre))
            {
                map  = new HashMap<String, String>();
                map.put("moyenneSemestre", line[0]);
                res.add(map);
            }
        }
        readerEtranger.close();
        return res;
    }

    @Override
    public List<Map> getInfosEnseignant(String login, String password) throws Exception
    {
        List<Map> res = new ArrayList<Map>();
        Map<String,String> map;
        String[] line;

        CSVReader readerEnseignant = new CSVReader(new FileReader(getClass().getResource(this.dataAccessString+"enseignant.txt").getFile()), ';');

        while((line = readerEnseignant.readNext()) != null)
        {
            if (line[3].equalsIgnoreCase(login) && line[4].equalsIgnoreCase(password))
            {
                map  = new HashMap<String, String>();
                map.put("unNom", line[0]);
                map.put("unPrenom", line[1]);
                map.put("nomDepartement", line[2]);
                res.add(map);
            }
        }
        readerEnseignant.close();
        return res;
    }

    @Override
    public List<Map> getLesDepartementsEnseignant(String nom, String prenom) throws Exception
    {
        List<Map> res = new ArrayList<Map>();
        Map<String,String> map;
        String[] line;

        CSVReader readerDepartement = new CSVReader(new FileReader(this.dataAccessString+"departement.txt"), ';');

        while((line = readerDepartement.readNext()) != null)
        {
            if(line.length > 2)
            {
                if (line[1].equalsIgnoreCase(nom) && line[2].equalsIgnoreCase(prenom))
                {
                    map  = new HashMap<String, String>();
                    map.put("unNom", line[0]);
                    res.add(map);
                }
            }
        }
        readerDepartement.close();
        return res;
    }

    @Override
    public List<Map> getLesAnneesEnseignant(String nom, String prenom) throws Exception
    {
        List<Map> res = new ArrayList<Map>();
        Map<String,String> map;
        String[] line;

        CSVReader readerAnnee = new CSVReader(new FileReader(this.dataAccessString+"annee.txt"), ';');

        while((line = readerAnnee.readNext()) != null)
        {
            if(line.length > 2)
            {
                if (line[1].equalsIgnoreCase(nom) && line[2].equalsIgnoreCase(prenom))
                {
                    map  = new HashMap<String, String>();
                    map.put("unNom", line[0]);
                    res.add(map);
                }
            }
        }
        readerAnnee.close();
        return res;
    }

    @Override
    public List<Map> getLesUEEnseignant(String nom, String prenom) throws Exception
    {
        List<Map> res = new ArrayList<Map>();
        Map<String,String> map;
        String[] line;

        CSVReader readerUE = new CSVReader(new FileReader(this.dataAccessString+"ue.txt"), ';');

        while((line = readerUE.readNext()) != null)
        {
            if(line.length > 2)
            {
                if (line[1].equalsIgnoreCase(nom) && line[2].equalsIgnoreCase(prenom))
                {
                    map  = new HashMap<String, String>();
                    map.put("unNom", line[0]);
                    res.add(map);
                }
            }
        }
        readerUE.close();
        return res;
    }

    @Override
    public List<Map> getLesECUEEnseignant(String nom, String prenom) throws Exception
    {
        List<Map> res = new ArrayList<Map>();
        Map<String,String> map;
        String[] line;

        CSVReader readerECUE = new CSVReader(new FileReader(this.dataAccessString+"ecue.txt"), ';');

        while((line = readerECUE.readNext()) != null)
        {
            if(line.length > 2)
            {
                if (line[1].equalsIgnoreCase(nom) && line[2].equalsIgnoreCase(prenom))
                {
                    map  = new HashMap<String, String>();
                    map.put("unNom", line[0]);
                    res.add(map);
                }
            }
        }
        readerECUE.close();
        return res;
    }

    @Override
    public String existeNote(String nom, String prenom, String nomECUE) throws Exception
    {
        Map<String,String> map;
        String[] line;

        CSVReader readerEtudiant = new CSVReader(new FileReader(this.dataAccessString+"etudiant.txt"), ';');
        int i = 0;

        while((line = readerEtudiant.readNext()) != null)
        {
            if (line[0].equalsIgnoreCase(nom) && line[1].equalsIgnoreCase(prenom) && line[9].equalsIgnoreCase(nomECUE))
            {
                i++;
            }
        }
        readerEtudiant.close();
        return Integer.toString(i);
    }

    @Override
    public void updateNote(String nom, String prenom, String nomECUE, String note, String estRattrapage) throws Exception
    {
        CSVReader readerEtudiant = new CSVReader(new FileReader(this.dataAccessString+"etudiant.txt"), ';');
        CSVWriter writerEtudiant = new CSVWriter(new FileWriter(this.dataAccessString+"etudiantcopie.txt"), ';', CSVWriter.NO_QUOTE_CHARACTER);
        String[] line;
        String[] col = new String[12];

        while ((line = readerEtudiant.readNext()) != null)
        {
            System.arraycopy(line, 0, col, 0, 12);
            if (line[0].equalsIgnoreCase(nom) && line[1].equalsIgnoreCase(prenom) && line[9].equalsIgnoreCase(nomECUE))
            {
                col[10] = note;
                col[11] = estRattrapage;
            }
            writerEtudiant.writeNext(col);
        }
        readerEtudiant.close();
        writerEtudiant.close();

        new File(this.dataAccessString+"etudiant.txt").delete();
        new File(this.dataAccessString+"etudiantcopie.txt").renameTo(new File(this.dataAccessString+"etudiant.txt"));
    }

    @Override
    public void insertNote(String nom, String prenom, String nomECUE, String note, String estRattrapage) throws Exception
    {
        CSVReader readerEtudiant = new CSVReader(new FileReader(this.dataAccessString+"etudiant.txt"), ';');
        CSVWriter writerEtudiant = new CSVWriter(new FileWriter(this.dataAccessString+"etudiantcopie.txt"), ';', CSVWriter.NO_QUOTE_CHARACTER);
        String[] line;
        String[] col = new String[12];

        while ((line = readerEtudiant.readNext()) != null)
        {
            System.arraycopy(line, 0, col, 0, 12);
            if (line[0].equalsIgnoreCase(nom) && line[1].equalsIgnoreCase(prenom) && line[9].equalsIgnoreCase(nomECUE))
            {
                col[10] = note;
                col[11] = estRattrapage;
            }
            writerEtudiant.writeNext(col);
        }
        readerEtudiant.close();
        writerEtudiant.close();

        new File(this.dataAccessString+"etudiant.txt").delete();
        new File(this.dataAccessString+"etudiantcopie.txt").renameTo(new File(this.dataAccessString+"etudiant.txt"));
    }

    @Override
    public void deleteAllAPDJ(String nom, String prenom) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void insertAPDJ(String nom, String prenom, String nomUE) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void deleteAllPointJurySemestre(String nom, String prenom) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void deleteAllPointJuryAnnee(String nom, String prenom) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }    

    @Override
    public void insertPointJuryAnnee(String nom, String prenom, String nbPoint) throws Exception
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void insertPointJurySemestre(String nom, String prenom, String codeSemestre, String nbPoint) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Map getLeSemestreUE(String nomUE) throws Exception
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Map getLeSemestreECUE(String nomECUE) throws Exception
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Map getUEECUE(String nomECUE) throws Exception
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Map getECUE(String nomECUE) throws Exception
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Map getAnneeUE(String nomUE) throws Exception
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Map getAnneeECUE(String nomECUE) throws Exception
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
